"""
Case Type   : bugfix
Case Name   : (隐式)类型转换时openGauss与MySQL的严格模式模式行为一致
Create At   : 2025/6/12
Owner       : liu-tong-8848
Description :
    1、B库建表插入数据
    2、执行sql
    3、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BugFix(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbBmode')
        self.user_node = Node('PrimaryDbBmode')
        self.constant = Constant()
        sql_cmd = "show dolphin.sql_mode;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertNotIn("ERROR", sql_res, '执行失败')
        self.dolphin_sql_mode = sql_res.strip().splitlines()[-2]

    def test_dolphin(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = '----step1:B库建表插入数据；expect:成功----'
        self.log.info(text)
        sql_cmd = f"set dolphin.sql_mode = 'sql_mode_strict';"\
                  + f"drop table if exists \`t char\`; create table \`t char\`(name char(4)); " \
                  + f"insert into \`t char\` values('1'); insert into \`t char\` values('1234'); " \
                  + f"insert into \`t char\` values('test'); select * from \`t char\`;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, sql_res, '执行失败' + text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, sql_res, '执行失败' + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertIn('1', sql_res, '执行失败' + text)
        self.assertIn('1234', sql_res, '执行失败' + text)
        self.assertIn('test', sql_res, '执行失败' + text)
        self.assertIn('(3 rows)', sql_res, '执行失败' + text)

        text = '----step2:执行sql；expect:成功----'
        self.log.info(text)
        sql_cmd = f"set dolphin.sql_mode = 'sql_mode_strict'; select * from \`t char\` where name = 1234;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertIn('WARNING', sql_res, '执行失败' + text)
        self.assertIn('invalid input syntax for type double precision: "test"', sql_res, '执行失败' + text)

    def tearDown(self):
        text = '----step3:删除表；expect:成功----'
        self.log.info(text)
        sql_cmd = f"set dolphin.sql_mode = 'sql_mode_strict'; drop table if exists \`t char\`; " \
                  + f"set dolphin.sql_mode = '{self.dolphin_sql_mode}';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, sql_res, '执行失败' + text)
        self.log.info(f'----{os.path.basename(__file__)} end-----')
